package gptma.fo.controler.request;

import gptma.bo.model.project.Project;
import gptma.bo.model.request.ClientRequest;
import gptma.bo.model.request.RequestType;
import gptma.bo.search.ClientRequestSearchCriteria;
import gptma.bo.services.request.ClientRequestServices;
import gptma.fo.controler.NavigationRule;
import gptma.fo.controler.ResultPage;
import java.io.Serializable;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang.StringUtils;

@Named
@ConversationScoped
public class RequestSearchPage extends ResultPage<ClientRequest> implements
		Serializable {

	private static final long serialVersionUID = -4420399374165792294L;

	@EJB
	private ClientRequestServices clientRequestServices;

	@Inject
	private RequestDetailsPage requestDetailsPage;

	private String reference;

	private String title;
	private RequestType type;
	private List<Project> projects;
	private Date dateFrom;
	private Date dateTo;

	public List<String> completeRequestReference(String partialReference) {
		return clientRequestServices.completeRequestReference(partialReference);
	}

	public NavigationRule directSearch() {
		NavigationRule navRule = null;
		int index = reference.indexOf("-") - 1;
		if (index > 0) {
			reference = reference.substring(0, index);
		}

		ClientRequest req = clientRequestServices.findByReference(reference);
		if (req != null) {
			requestDetailsPage.setClientRequest(req);
			navRule = NavigationRule.REQ_DETAILS;
		} else {
			addErrorMessageFromKey("errPersistence");
		}

		return navRule;
	}

	public NavigationRule search() {
		results = null;

		if (StringUtils.isEmpty(title) && type == null
				&& (projects == null || projects.isEmpty()) && dateFrom == null
				&& dateTo == null) {
			addInfoMessageFromKey("dclRechercheNoCriteria");
		} else {
			ClientRequestSearchCriteria crit = new ClientRequestSearchCriteria();
			crit.setTitle(title);
			crit.setRequestType(type);
			crit.setProjects(projects);
			crit.setCreationDateFrom(dateFrom);
			crit.setCreationDateTo(dateTo);

			results = clientRequestServices.findByCriteria(crit);

			if (results.isEmpty()) {
				addInfoMessageFromKey("infoNoResultFound");
			}
		}
		return null;
	}

	public List<RequestType> getTypes() {
		return Arrays.asList(RequestType.values());
	}

	/**
	 * @return the reference
	 */
	public String getReference() {
		return reference;
	}

	/**
	 * @param reference
	 *            the reference to set
	 */
	public void setReference(String reference) {
		this.reference = reference;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the type
	 */
	public RequestType getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(RequestType type) {
		this.type = type;
	}

	/**
	 * @return the dateFrom
	 */
	public Date getDateFrom() {
		return dateFrom;
	}

	/**
	 * @param dateFrom
	 *            the dateFrom to set
	 */
	public void setDateFrom(Date dateFrom) {
		this.dateFrom = dateFrom;
	}

	/**
	 * @return the dateTo
	 */
	public Date getDateTo() {
		return dateTo;
	}

	/**
	 * @param dateTo
	 *            the dateTo to set
	 */
	public void setDateTo(Date dateTo) {
		this.dateTo = dateTo;
	}

	/**
	 * @return the projects
	 */
	public List<Project> getProjects() {
		return projects;
	}

	/**
	 * @param projects
	 *            the projects to set
	 */
	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}

}
